package com.norma.macaron.service.oauth.service;

import com.norma.macaron.common.model.enums.CommonCode;
import com.norma.macaron.common.model.enums.CredentialType;
import com.norma.macaron.common.model.response.ResponseT;
import com.norma.macaron.service.common.client.user.UserServiceClient;
import com.norma.macaron.service.common.domain.auth.UserExt;
import com.norma.macaron.service.common.domain.user.TUser;
import com.norma.macaron.service.oauth.domain.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhangchaofeng
 * @Date 16:28 2020/6/8 0008
 **/
@Service
public class UserDetailService implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserServiceClient userServiceClient;

//    @PostConstruct
//    public void initData() {
//        String password = passwordEncoder.encode("123456");
//        loginUserList = new ArrayList<>();
//        loginUserList.add(new LoginUser("macro", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin")));
//        loginUserList.add(new LoginUser("andy", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
//        loginUserList.add(new LoginUser("mark", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
//    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /**为了支持多终端类型登录 userName后面需要拼装登录类型  userName | type */
        LoginUser loginUser = null;
        String[] params = username.split("\\|");
        //real username
        username = params[0];
        UserExt userExt = userServiceClient.getUserExtByPhone(username);
        if (userExt == null || StringUtils.isBlank(userExt.getPhone())) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        loginUser = new LoginUser(userExt.getPhone(), userExt.getPassword(), userExt.getRoles(), userExt.getPermissions());
        if (params.length > 1) {
            CredentialType credentialType = CredentialType.valueOf(params[1]);
            if (CredentialType.PHONE == credentialType) {
                //手机 + 短信登录
                handlerPhoneSmsLogin(loginUser, params);
            } else if (CredentialType.WECHAT_OPENID == credentialType) {
                // 微信登录
                handlerWechatLogin(loginUser, params);
            }
        }
        return loginUser;
    }

    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123"));

    }

    private void handlerPhoneSmsLogin(LoginUser loginUser, String[] params) {
        if (params.length < 3) {
            throw new IllegalArgumentException("参数非法");
        }
        String phone = params[0];
        String smsCode = params[2];
        //TODO 验证短信验证码
        /**短信登录无需密码,若短信验证码正确，则修改loginUser的密码为手机号，前台用相同的处理逻辑*/
        loginUser.setPassword(passwordEncoder.encode(phone));
    }

    private void handlerWechatLogin(LoginUser loginUser, String[] params) {


    }


}
